package main.java.controller;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import main.java.entity.InstanceEntity;
import main.java.entity.TreeEntity;
import main.java.service.LibraryService;
import net.sf.json.JSONArray;

@Controller
@RequestMapping(value = "/library")
public class LibraryController {

	@Resource
	public LibraryService libraryService;

	/**
	* 跳转到分类页面
	* @return
	*/
	@RequestMapping(value = "toListLibrary.do")
	public String toListLibrary() {
		return "library/listLibrary";
	}

	/**
	* 查询所有分类信息
	* @return
	*/
	@RequestMapping(value = "findNodesByParentId.do")
	@ResponseBody
	public JSONArray findNodesByParentId(HttpServletRequest request, HttpServletResponse response) {
//		System.out.println("ztree");
		//前台传过来的id
		String id = request.getParameter("id");
		List<TreeEntity> objects = new ArrayList<TreeEntity>();
//		JSONObject object=new JSONObject;
		if(StringUtils.isNotBlank(id)) {
			objects = libraryService.findNodesByParentId(id);
		}else {
			id="0";
			objects = libraryService.findNodesByParentId(id);
		}
		JSONArray array=new JSONArray();
		for (TreeEntity object : objects) {
			array.add(object);
		}
		return array;
	}
	
	
	/**
	* @return
	*/
	@RequestMapping(value = "mdfindNodesByParentId.do")
	@ResponseBody
	public JSONArray mdfindNodesByParentId(HttpServletRequest request, HttpServletResponse response) {
//		System.out.println("ztree");
		//前台传过来的id
		String id = request.getParameter("id");
		String clasfCd=request.getParameter("clasfCd");
		InstanceEntity paramEntity=new InstanceEntity();
		paramEntity.setClasf_cd(clasfCd);
		List<TreeEntity> objects = new ArrayList<TreeEntity>();
//		JSONObject object=new JSONObject;
		if(StringUtils.isNotBlank(id)) {
			paramEntity.setId(id);
			objects = libraryService.mdFindNodesByParentId(paramEntity);
		}else {
			id="";
			paramEntity.setId(id);
			objects = libraryService.mdFindNodesByParentId(paramEntity);
		}
		JSONArray array=new JSONArray();
		for (TreeEntity object : objects) {
			array.add(object);
		}
		return array;
	}
}
